﻿
@{
    ViewBag.Title = "确认订单";
    Layout = "~/Views/Shared/_MallLayout.cshtml";
    var img_url = ViewBag.img_url;
    string wxOpenId = ViewBag.wxOpenId;
    shipping_address_dto address = null;
    if (ViewBag.address != null) { address = ViewBag.address as shipping_address_dto; }
}
@model token_bazaar_product_dto
    <style type="text/css">
        .page-order .orderAddress {
            padding: 0 0 .4266666666666667rem;
        }
            .page-order .orderAddress .box {
                position: relative;
                padding: .8533333333333334rem 2.1333333333333333rem .8533333333333334rem .64rem;
                background: #fff;
            }
            .page-order .orderAddress .lineBg {
                position: absolute;
                left: 0;
                bottom: 0;
                width: 100%;
                background: url(/Content/css/tokenmall/img/lineBg.png) -8px bottom repeat-x #fff;
                background-size: auto 100%;
                height: .21333333333333335rem;
            }
            .page-order .orderAddress .icon-go {
                position: absolute;
                right: .64rem;
                top: 50%;
                margin-top: -.32rem;
            }
            .page-order .orderAddress .top {
                margin: 0 0 .4266666666666667rem;
            }
        .page-confirmOrder .lt-bottom .ui-btn-buy {
            float: right;
            width: 5.12rem;
        }
    </style>
<body class="page-order page-confirmOrder">
    @*page-confirmOrder*@
    <div class="orderAddress mt20 ">
        <a href="/user/MyAdress?returnUrl=@(Url.Encode(Request.Url.ToString()))">
            @if (address != null) {
                <div class="box">
                    <div class="lineBg"></div>
                    <i class="icon-go"></i>
                    <div class="top fn30 fn-pfBold">
                        <span class="name">@address.ship_to</span>
                        @address.phone
                    </div>
                    <p class="col-gray fn28">@(address.region_name + address.address)</p>
                </div>
            } else {
                <div class="box">
                    <div class="lineBg"></div>
                    <i class="icon-go"></i>
                    <div class="top fn30 fn-pfBold">
                        <span class="name"></span>
                    </div>
                    <p class="col-gray fn28">请选择收货地址</p>
                </div>
            }
        </a>
    </div>

    <div class="view">
        <ul>
            <li>
                <div class="img">
                    <img src="@(img_url+Model.main_img)" />
                </div>
                <div class="text">
                    <p class="fn28 fn-pfBold">
                        @Model.product_name
                    </p>
                    <div class="price col-gray mt30">
                        价格：￥
                        <span class="col-red fn48 fn-pfBold priceLi">@Model.market_price.ToString("f2")</span>
                        @*红钻*@
                    </div>
                </div>
            </li>
        </ul>
    </div>
    <input type="hidden" id="wxOpenId" value="@wxOpenId" />
    <div class="lt-bottom">
        <div class="right">
            <span class="fn28 fn-left">
                    总金额：
                    ￥<em class="col-red fn36 fn-pfBold totalLi">@(Model.market_price.ToString("f2"))</em>
                    @*红钻*@
                </span>
            <a href="javascript:;" class="ui-btn-buy" id="buyNow">
                立即购买
            </a>
        </div>
    </div>


    <script src="~/Scripts/token-common.js"></script>
    <script type="text/javascript">

        function onBridgeReady(wcPayModel) {
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest', {
                    "appId": wcPayModel.appid,
                    "timeStamp": wcPayModel.timestamp,
                    "nonceStr": wcPayModel.noncestr,
                    "package": wcPayModel.package,
                    "signType": "MD5",
                    "paySign": wcPayModel.paysign
                },
                function (res) {
                    if (res.err_msg == "get_brand_wcpay_request:ok") {// 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
                        window.location.href = '/MyOrder/OrderList?type=2';
                    } else {
                        window.location.href = '/MyOrder/OrderList?type=2';
                    }
                }
            );
        }
            $("#buyNow").click(function () {
                if ('@(address== null)' == 'True') {
                    layer.msg("请选择收货地址");
                    return;
                }

                var flag = false;
                layer.load(1);

        if (flag) return;
        flag = true;
        $.ajax({
            //几个参数需要注意一下
            type: "POST",//方法类型
            dataType: "json",//预期服务器返回的数据类型
            url: "/BazaarOrder/Pay",//url
            data: { id: @Model.id, addressId:@(address!=null?address.id:0) , wxOpenId: $("#wxOpenId").val()},
            success: function (result) {
                layer.closeAll('loading');
                if (result.result_code == 1000) {
                    if (isWxBrowser() == true) {//微信浏览器打开，公众号支付
                        //layer.msg("请在微信外浏览器打开完成支付");
                        //return false;
                        var wcPayModel = $.parseJSON(result.result_msg);
                        if (typeof WeixinJSBridge == "undefined") {
                            if (document.addEventListener) {
                                document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                            } else if (document.attachEvent) {
                                document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                                document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                            }
                        } else {
                            onBridgeReady(wcPayModel);
                        }
                    } else {//h5支付
                        window.location.href = result.result_msg;
                    }
                }
                else {
                    layer.msg(result.result_msg);
                }
                flag = false;
            },
            error: function () {
                layer.msg("异常！");
                flag = false;
            }
        });
            });
    </script>
</body>

